package com.road.vast.system.permission.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.road.vast.common.entity.ResultCode;
import com.road.vast.common.exception.CommonException;
import com.road.vast.common.utils.BeanMapUtils;
import com.road.vast.common.utils.PermissionConstants;
import com.road.vast.pojo.system.*;
import com.road.vast.system.component.IdWorkerComponent;
import com.road.vast.system.permission.mapper.*;
import com.road.vast.system.permission.service.PePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限信息 服务实现类
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-07-17
 */
@Service
@Transactional
public class PePermissionServiceImpl extends ServiceImpl<PePermissionMapper, PePermission> implements PePermissionService {

    @Autowired
    private IdWorkerComponent idWorkerComponent;

    @Resource
    private PePermissionMapper pePermissionMapper;

    @Resource
    private PePermissionMenuMapper pePermissionMenuMapper;

    @Resource
    private PePermissionPointMapper pePermissionPointMapper;

    @Resource
    private PePermissionApiMapper pePermissionApiMapper;

    @Resource
    private PeRolePermissionMapper rolePermissionMapper;

    @Override
    public Integer savePermission(Map<String, Object> map) throws Exception {
        //生成主键
        String nextId = idWorkerComponent.getIdWorker().nextId() + "";
        //通过Map构造Permission对象
        PePermission permission = BeanMapUtils.mapToBean(map, PePermission.class);
        permission.setId(nextId);
        //2.根据类型构造不同的资源对象
        Integer type = permission.getType();
        switch (type){
            case PermissionConstants.PY_MENU:
                PePermissionMenu permissionMenu = BeanMapUtils.mapToBean(map, PePermissionMenu.class);
                permissionMenu.setId(nextId);
                pePermissionMenuMapper.insert(permissionMenu);
                break;
            case PermissionConstants.PY_POINT:
                PePermissionPoint permissionPoint = BeanMapUtils.mapToBean(map, PePermissionPoint.class);
                permissionPoint.setId(nextId);
                pePermissionPointMapper.insert(permissionPoint);
                break;
            case PermissionConstants.PY_API:
                PePermissionApi permissionApi = BeanMapUtils.mapToBean(map, PePermissionApi.class);
                permissionApi.setId(nextId);
                pePermissionApiMapper.insert(permissionApi);
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }
        return pePermissionMapper.insert(permission);
    }

    @Override
    public Integer updatePermission(Map<String, Object> map) throws Exception {
        //通过Map构造Permission对象
        PePermission permission = BeanMapUtils.mapToBean(map, PePermission.class);
        //2.根据类型构造不同的资源对象
        Integer type = permission.getType();
        switch (type){
            case PermissionConstants.PY_MENU:
                PePermissionMenu permissionMenu = BeanMapUtils.mapToBean(map, PePermissionMenu.class);
                permissionMenu.setId(permission.getId());
                pePermissionMenuMapper.updateById(permissionMenu);
                break;
            case PermissionConstants.PY_POINT:
                PePermissionPoint permissionPoint = BeanMapUtils.mapToBean(map, PePermissionPoint.class);
                permissionPoint.setId(permission.getId());
                pePermissionPointMapper.updateById(permissionPoint);
                break;
            case PermissionConstants.PY_API:
                PePermissionApi permissionApi = BeanMapUtils.mapToBean(map, PePermissionApi.class);
                permissionApi.setId(permission.getId());
                pePermissionApiMapper.updateById(permissionApi);
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }
        return pePermissionMapper.updateById(permission);
    }

    @Override
    public List<PePermission> getAll(Map<String, Object> map) {
        QueryWrapper<PePermission> wrapper = new QueryWrapper<>();
        //父id
        if (!StringUtils.isEmpty((CharSequence) map.get("parentId"))){
            wrapper.eq("parent_id", map.get("parentId"));
        }
        //权限
        if (!StringUtils.isEmpty((CharSequence) map.get("enVisible"))){
            wrapper.eq("en_visible", map.get("enVisible"));
        }
        //查询全部权限列表type：0：菜单 + 按钮（权限点） 1：菜单2：按钮（权限点）3：API接 口
        if(!StringUtils.isEmpty((CharSequence) map.get("type"))) {
            String ty = (String) map.get("type");
            if ("0".equals(ty)){
                wrapper.in("type", "1", "2");
            }else{
                wrapper.in("type", ty);
            }
        }
        return pePermissionMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> getById(String id) throws CommonException {
        PePermission permission = pePermissionMapper.selectById(id);
        Integer type = permission.getType();

        Object object = new Object();

        if(type == PermissionConstants.PY_MENU){
            object = pePermissionMenuMapper.selectById(id);
        }else if (type == PermissionConstants.PY_POINT){
            object = pePermissionPointMapper.selectById(id);
        }else if (type == PermissionConstants.PY_API){
            object = pePermissionApiMapper.selectById(id);
        }else{
            throw new CommonException(ResultCode.FAIL);
        }
        Map<String, Object> map = BeanMapUtils.beanToMap(object);
        map.put("name", permission.getName());
        map.put("type", permission.getType());
        map.put("code", permission.getCode());
        map.put("description", permission.getDescription());
        map.put("parentId", permission.getParentId());
        map.put("enVisible", permission.getEnVisible());
        return map;
    }

    @Override
    public Integer deletePermission(String id) throws CommonException {
        PePermission permission = pePermissionMapper.selectById(id);
        //2.根据类型删除不同的资源对象
        Integer type = permission.getType();
        switch (type){
            case PermissionConstants.PY_MENU:
                pePermissionMenuMapper.deleteById(id);
                break;
            case PermissionConstants.PY_POINT:
                pePermissionPointMapper.deleteById(id);
                break;
            case PermissionConstants.PY_API:
                pePermissionApiMapper.deleteById(id);
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }
        //删除权限表信息
        return pePermissionMapper.deleteById(id);
    }
}
